layui.define(['form', 'table'], function(exports) {
    var $ = layui.$, table = layui.table, form = layui.form;

    form.on('submit(LAY-config-submit)', function (data) {
        var field = data.field;
        layer.confirm('确定要保存吗？', function (index) {
            $.ajax({
                type: "post",
                url: layui.setter.webRoot + "/cdcz/device/cost/save.do",
                data: field,
                dataType: "json",
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg('保存成功', {icon: 1});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }
            });
        });
    });

    table.render({
        elem: '#LAY-table'
        ,url: layui.setter.webRoot + '/cdcz/device/cost/power_interval/list.data'
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'minPower', title: '最低功率'}
            ,{field: 'maxPower', title: '最大功率'}
            ,{field: 'moneyStr', title: '每小时收费(元)'}
            ,{title: '操作', width: 150, align:'center', fixed: 'right',templet:function (record){
                    var html = ' <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="updatePowerItem">修改</a>';
                    html = html + ' <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delPowerItem">删除</a>';
                    return html;
                }}
        ]]
        ,page: false
        ,text: '对不起，加载出现异常！'
    });

    var active = {
        addPowerItem: function () {
            layer.open({
                type: 2
                , title: '新增功率区间费用'
                , content: layui.setter.webRoot + '/cdcz/device/cost/power_interval/add.html'
                , maxmin: false
                , area: ['500px', '500px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-form-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                        var field = data.field;
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/cdcz/device/cost/power_interval/add.do',
                            data: field,
                            dataType: "json",
                            success: function (res) {
                                if (res.code == 200) {
                                    layer.close(index); //关闭弹层
                                    layer.msg('新增成功', {icon: 1});
                                    table.reload('LAY-table');
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
        , updatePowerItem: function (obj) {
            layer.open({
                type: 2,
                title: '修改功率区间费用',
                content: layui.setter.webRoot + '/cdcz/device/cost/power_interval/update.html?minPower=' + obj.data.minPower,
                maxmin: false,
                area: ['500px', '500px'],
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-form-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                        var field = data.field;
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/cdcz/device/cost/power_interval/update.do',
                            data: field,
                            dataType: "json",
                            success: function (res) {
                                if (res.code == 200) {
                                    layer.close(index); //关闭弹层
                                    layer.msg('修改成功', {icon: 1});
                                    table.reload('LAY-table');
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
        ,delPowerItem: function (obj) {
            layer.confirm('确定要删除该功率区间费用吗？', function (index) {
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/cdcz/device/cost/power_interval/del.do',
                    data: {minPower: obj.data.minPower},
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 200) {
                            layer.msg('删除成功', {icon: 1});
                            table.reload('LAY-table');
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
    }

    $('.layui-btn.LAY-btn-fun').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    table.on('tool(LAY-table)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });

    exports('cdcz_cost_index', {})

})